package com.huilian.cloud.module.system.service.frontendbackendpermission;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.huilian.cloud.module.system.controller.admin.frontendbackendpermission.vo.*;
import com.huilian.cloud.module.system.dal.dataobject.frontendbackendpermission.FrontendBackendPermissionDO;
import com.huilian.cloud.framework.common.pojo.PageResult;
import com.huilian.cloud.framework.common.pojo.PageParam;
import com.huilian.cloud.framework.common.util.object.BeanUtils;

import com.huilian.cloud.module.system.dal.mysql.frontendbackendpermission.FrontendBackendPermissionMapper;

import static com.huilian.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.huilian.cloud.module.system.enums.ErrorCodeConstants.*;

/**
 * 前后端关联标识 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class FrontendBackendPermissionServiceImpl implements FrontendBackendPermissionService {

    @Resource
    private FrontendBackendPermissionMapper frontendBackendPermissionMapper;

    @Override
    public String createFrontendBackendPermission(FrontendBackendPermissionSaveReqVO createReqVO) {
        // 插入
        FrontendBackendPermissionDO frontendBackendPermission = BeanUtils.toBean(createReqVO, FrontendBackendPermissionDO.class);
        frontendBackendPermissionMapper.insert(frontendBackendPermission);
        // 返回
        return frontendBackendPermission.getFrontendPermission();
    }

    @Override
    public void updateFrontendBackendPermission(FrontendBackendPermissionSaveReqVO updateReqVO) {

        // 更新
        FrontendBackendPermissionDO updateObj = BeanUtils.toBean(updateReqVO, FrontendBackendPermissionDO.class);
        frontendBackendPermissionMapper.updateById(updateObj);
    }

    @Override
    public void deleteFrontendBackendPermission(String id) {
        // 校验存在
        validateFrontendBackendPermissionExists(id);
        // 删除
        frontendBackendPermissionMapper.deleteById(id);
    }

    private void validateFrontendBackendPermissionExists(String id) {
        if (frontendBackendPermissionMapper.selectById(id) == null) {
            throw exception(FRONTEND_BACKEND_PERMISSION_NOT_EXISTS);
        }
    }

    @Override
    public FrontendBackendPermissionDO getFrontendBackendPermission(String id) {
        return frontendBackendPermissionMapper.selectById(id);
    }

    @Override
    public PageResult<FrontendBackendPermissionDO> getFrontendBackendPermissionPage(FrontendBackendPermissionPageReqVO pageReqVO) {
        return frontendBackendPermissionMapper.selectPage(pageReqVO);
    }

}